﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.RepositoryFramework;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain;
using Ue.Qcdz.Domain.Membership;
using Ue.Qcdz.Domain.SaleCategorys;
using Ue.Qcdz.Domain.SaleInfos;
using Ue.Qcdz.Domain.Shops;
using TextBox = System.Web.UI.WebControls.TextBox;

namespace Ue.Qcdz.UI.Controls.SaleInfos
{
    public class SaleInfoEdit : SkinnedEntityEdit<Ue.Qcdz.Domain.SaleInfos.SaleInfo>
    {
        private User user;
        private SaleInfoTypeRadio saleInfoTypeRadio1;
        private TextBox tbxSalteTitle;
        private TextBox tbxKeyWord;
        private PeriodRdaio periodRdaio1;
        private TextBox tbxDirectory;
        private TextBox tbxBrand;
        private TextBox tbxModel;
        private TextBox tbxDescription;
        private TextBox tbxImageUrl;
        private DropDownList SaleCategorydrop;
        private Shop shopItem;
        private string skinFileName = "~/Themes/zh-cn/Skins/SaleInfo/SaleInfoEdit.ascx";
        #region Overrides of SkinnedControl

        protected override string GetSkinFileName()
        {
            return skinFileName;
        }

        #endregion

        #region Overrides of SkinnedEntityEdit<SaleInfo>
        private Shop GetShop(User user)
        {
            IShopRepository repository = RepositoryFactory.GetRepository<IShopRepository, Shop>();
            return repository.GetShop(user);
        }
        private void BindSaleCategory()
        {
            ISaleCategoryRepository repository = RepositoryFactory.GetRepository<ISaleCategoryRepository, SaleCategory>();
            List<SaleCategory> list = (List<SaleCategory>)repository.FindBy(shopItem.Key.ToString(), 1);
            foreach (SaleCategory saleCategory in list)
            {
                ListItem listItem = new ListItem(saleCategory.Name, saleCategory.Key.ToString());
                SaleCategorydrop.Items.Add(listItem);
            }
        }

        private bool IsValidation()
        {

            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return true;
            }
            else
            {
                MessageWindow.RenderMessage("请您登陆");
                return false;
            }


        }
        private bool CheckResource(User user)
        {
            ISaleInfoRepository repository = Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<ISaleInfoRepository, Ue.Qcdz.Domain.SaleInfos.SaleInfo>();
            int num = repository.GetCount(user);
            int tempNum = 0;
            foreach (var role in user.Roles)
            {
                foreach (var Resource in role.ResourceList)
                {
                    if (Resource.Num > tempNum) tempNum = Resource.Num;
                }
            }
            return tempNum > num;
        }

        protected override void InitSkin(Control skin)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                user = SiteContext.GetSiteContext().User;

                shopItem = GetShop(user);

                saleInfoTypeRadio1 = skin.FindControl<SaleInfoTypeRadio>("saleInfoTypeRadio1");
                tbxSalteTitle = skin.FindControl<TextBox>("tbxSalteTitle");
                tbxKeyWord = skin.FindControl<TextBox>("tbxKeyWord");
                periodRdaio1 = skin.FindControl<PeriodRdaio>("periodRdaio1");
                tbxDirectory = skin.FindControl<TextBox>("tbxDirectory");
                tbxBrand = skin.FindControl<TextBox>("tbxBrand");
                tbxModel = skin.FindControl<TextBox>("tbxModel");
                tbxDescription = skin.FindControl<TextBox>("tbxDescription");
                tbxImageUrl = skin.FindControl<TextBox>("tbxImageUrl");
                SaleCategorydrop = skin.FindControl<DropDownList>("SaleCategorydrop");
                BindSaleCategory();

                periodRdaio1.SelectedIndex = periodRdaio1.Items.Count - 1;
            }
            else
            {
                MessageWindow.RenderMessage("请您先登录", "../login.aspx");
            }
        }

        protected override Ue.Qcdz.Domain.SaleInfos.SaleInfo Enttiy
        {
            get
            {


                if (this.Action == "Delete")
                {
                    if (IsValidation())
                    {
                        return new Ue.Qcdz.Domain.SaleInfos.SaleInfo(this.EntityKey);
                    }

                }
                if (CheckResource(user))
                {
                    int SalesType = (int)saleInfoTypeRadio1.SelectedValue;
                    string SalteTitle = tbxSalteTitle.GetValue<string>();
                    string KeyWord = tbxKeyWord.GetValue<string>();
                    PeriodDate Period = periodRdaio1.SelectedValue;
                    string Directory = tbxDirectory.GetValue<string>();
                    string Brand = tbxBrand.GetValue<string>();
                    string Model = tbxModel.GetValue<string>();
                    string Description = tbxDescription.GetValue<string>();
                    string PicUrl = tbxImageUrl.GetValue<string>();
                    string shopId = shopItem.Key.ToString();
                    int periodDate = Convert.ToInt32(periodRdaio1.SelectedValue);
                    DateTime Deadline = DateTime.Now.AddDays(periodDate);
                    string SaleCategoryId = SaleCategorydrop.SelectedValue;
                    var saleInfo =
                        new Ue.Qcdz.Domain.SaleInfos.SaleInfo(SiteContext.GetSiteContext().User.Key.ToString(),
                                                              (SaleInfoEnum)SalesType, SalteTitle, KeyWord, Period,
                                                              Directory, Brand, Model, Description, PicUrl, 0, 0,
                                                              shopId, SaleCategoryId, Deadline, this.EntityKey);
                    return saleInfo;
                }
                else
                {
                    MessageWindow.RenderMessage("发布次数已满", "/default.aspx");
                    return null;
                }

            }
            set
            {
                if (IsValidation())
                {
                    if (value == null)
                        return;
                    saleInfoTypeRadio1.SelectedValue = value.SalesType;
                    tbxSalteTitle.SetValue(value.SalteTitle);
                    tbxKeyWord.SetValue(value.KeyWord);
                    periodRdaio1.SelectedValue = value.Period;
                    tbxDirectory.SetValue(value.Directory);
                    tbxBrand.SetValue(value.Brand);
                    tbxModel.SetValue(value.Model);
                    tbxDescription.SetValue(value.Description);
                    tbxImageUrl.SetValue(value.PicUrl);
                    SaleCategorydrop.SelectedValue = value.SaleCategoryId;
                }

            }

        }

        #endregion
    }
}
